<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="本地化" tabindex="-1"><a class="header-anchor" href="#本地化"><span>本地化</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#configuring-the-locale">区域配置</a></li>
</ul>
</li>
<li><a href="#defining-translation-strings">定义翻译字符串</a>
<ul>
<li><a href="#using-short-keys">如何使用短键</a></li>
<li><a href="#using-translation-strings-as-keys">如何使用翻译字符串作为键</a></li>
</ul>
</li>
<li><a href="#retrieving-translation-strings">检索翻译字符串</a>
<ul>
<li><a href="#replacing-parameters-in-translation-strings">替换翻译字符串中的参数</a></li>
<li><a href="#pluralization">复数</a></li>
</ul>
</li>
<li><a href="#overriding-package-language-files">重写扩展包的语言文件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 本地化特性提供了从不同语言文件中检索字符串的简单方法，让您的应用更好地支持多语言开发。语言文件默认都放在 <code v-pre>resources/lang</code> 目录中。在此目录中，相应的语言文件存放在相应的子目录下，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>resources</span>
<span class="line">    <span class="token operator">/</span>lang</span>
<span class="line">        <span class="token operator">/</span>en</span>
<span class="line">            messages<span class="token operator">.</span>php</span>
<span class="line">        <span class="token operator">/</span>es</span>
<span class="line">            messages<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>所有的语言文件都返回一个键值对数组，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome to our application'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="区域设置" tabindex="-1"><a class="header-anchor" href="#区域设置"><span>区域设置</span></a></h3>
<p>应用的默认语言设置保存在 <code v-pre>config/app.php</code> 配置文件中。你可以根据需要修改当前设置，还可以使用 <code v-pre>App</code> Facade 的 <code v-pre>setLocale</code> 方法动态地更改应用运行过程中使用的语言：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'welcome/{locale}'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">setLocale</span><span class="token punctuation">(</span><span class="token variable">$locale</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以设置『备用语言』，它会在当前语言不包含给定的翻译字符串时被使用。像默认语言一样，备用语言也可以在 <code v-pre>config/app.php</code> 配置文件中设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'fallback_locale'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'en'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="确定当前语言环境" tabindex="-1"><a class="header-anchor" href="#确定当前语言环境"><span>确定当前语言环境</span></a></h4>
<p>你可以使用 <code v-pre>App</code> Facade的 <code v-pre>getLocale</code> 和 <code v-pre>isLocale</code> 方法确定当前的区域设置或者检查语言环境是否为给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$locale</span> <span class="token operator">=</span> <span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">getLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">isLocale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="定义翻译字符串" tabindex="-1"><a class="header-anchor" href="#定义翻译字符串"><span>定义翻译字符串</span></a></h2>
<h3 id="使用短键" tabindex="-1"><a class="header-anchor" href="#使用短键"><span>使用短键</span></a></h3>
<p>通常，翻译字符串都存放在 <code v-pre>resources/lang</code> 目录下的文件里。在此目录中，但凡应用支持的每种语言都应该有一个对应的子目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">/</span>resources</span>
<span class="line">    <span class="token operator">/</span>lang</span>
<span class="line">        <span class="token operator">/</span>en</span>
<span class="line">            messages<span class="token operator">.</span>php</span>
<span class="line">        <span class="token operator">/</span>es</span>
<span class="line">            messages<span class="token operator">.</span>php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>所有语言文件都返回键值对数组，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// resources/lang/en/messages.php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome to our application'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用翻译字符串作为键" tabindex="-1"><a class="header-anchor" href="#使用翻译字符串作为键"><span>使用翻译字符串作为键</span></a></h3>
<p>对于有大量翻译需求的应用，如果每条翻译语句都要一一使用 『短键』 来定义，那么当你在视图中尝试去引用这些 『短键』 的时候，很容易变得混乱。因此， Laravel 也支持使用字符串 『默认』 翻译作为关键字来定义翻译字符串。</p>
<p>使用翻译字符串作为键的翻译文件以 JSON 格式存储在 <code v-pre>resources/lang</code> 目录中。例如，如果你的应用中有西班牙语翻译，你应该在该目录下新建一个 <code v-pre>resources/lang/es.json</code> 文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"I love programming."</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"Me encanta programar."</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="检索翻译字符串" tabindex="-1"><a class="header-anchor" href="#检索翻译字符串"><span>检索翻译字符串</span></a></h2>
<p>你可以使用辅助函数 <code v-pre>__</code> 从语言文件中检索， <code v-pre>__</code> 函数接受翻译字符串所在的文件名加键名作为其第一个参数。例如，我们要检索 <code v-pre>resources/lang/messages.php</code> 语言文件中的翻译字符串 <code v-pre>welcome</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'I love programming.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你正使用 <a href="https://learnku.com/docs/laravel/6.x/blade" target="_blank" rel="noopener noreferrer">Blade 模板引擎</a> ，你可以在视图文件中使用 <code v-pre>{{ }}</code> 语法或者使用 <code v-pre>@lang</code> 指令来打印翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">lang</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果指定的翻译字符串不存在，那么 <code v-pre>__</code> 函数会直接返回该翻译字符串的键名。所以，如果上述示例中的翻译字符串对应的键值对不存在， <code v-pre>__</code> 函数将会直接返回 <code v-pre>messages.welcome</code> 。</p>
<blockquote>
<p>注意：<code v-pre>@lang</code> 指令不会对任何输出进行转义。当你使用这个指令时，你必须 <strong>完全由自己承担</strong> 对输出内容的转义工作。</p>
</blockquote>
<h3 id="翻译字符串中的参数替换" tabindex="-1"><a class="header-anchor" href="#翻译字符串中的参数替换"><span>翻译字符串中的参数替换</span></a></h3>
<p>如果需要，你可以在翻译字符串中定义占位符。所有的占位符都有一个 <code v-pre>:</code> 前缀。例如，你可以使用占位符 name 定义欢迎消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome, :name'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以在 <code v-pre>__</code> 函数中传递一个数组作为第二个参数，它会将数组中的值替换到翻译字符串的占位符中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dayle'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你的占位符中包含了首字母大写或者全部为大写，翻译过来的内容也会做相应的大写处理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'welcome'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Welcome, :NAME'</span><span class="token punctuation">,</span> <span class="token comment">// Welcome, DAYLE</span></span>
<span class="line"><span class="token string single-quoted-string">'goodbye'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Goodbye, :Name'</span><span class="token punctuation">,</span> <span class="token comment">// Goodbye, Dayle</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="复数" tabindex="-1"><a class="header-anchor" href="#复数"><span>复数</span></a></h3>
<p>复数是一个复杂的问题，因为不同的语言对复数有不同的规则， 使用 『管道符』 <code v-pre>|</code> ，可以区分字符串的单复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'There is one apple|There are many apples'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你甚至可以创建更复杂的复数规则，为多个数字范围指定翻译字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{0} There are none|[1,19] There are some|[20,*] There are many'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在定义具有复数选项的翻译字符串之后，你可以使用 <code v-pre>trans_choice</code> 函数来检索给定『数量』 的内容。例如, 设置 『总数』 为 10 ，符合数量范围 1 至 19 ，所以会得到 There are some 这条复数语句：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.apples'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以在复数字符串中插入占位符。 <code v-pre>trans_choice</code> 函数第三个参数所传递数组将会替换占位符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'minutes_ago'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{1} :value minute ago|[2,*] :value minutes ago'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'time.minutes_ago'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想让传递给 <code v-pre>trans_choice</code> 函数的 『数量』 参数显示在翻译字符串中，你可以使用 <code v-pre>:count</code> 占位符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'apples'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{0} There are none|{1} There is one|[2,*] There are :count'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="重写扩展包的语言文件" tabindex="-1"><a class="header-anchor" href="#重写扩展包的语言文件"><span>重写扩展包的语言文件</span></a></h2>
<p>部分扩展包可能会附带自己的语言文件。你可以通过在 <code v-pre>resources/lang/vendor/{package}/{locale}</code> 目录放置文件来重写它们，而不要直接修改扩展包的核心文件。</p>
<p>例如，当你需要重写 <code v-pre>skyrim/hearthfire</code> 扩展包的英语语言文件 <code v-pre>messages.php</code> ，则需要把文件存放为 <code v-pre>resources/lang/vendor/hearthfire/en/messages.php</code> 。在这个文件中，你只需要定义你想要修改的翻译字符串。任何没有被重写的翻译字符串仍将从扩展包的原始语言文件中加载。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/localization/5148" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/loc...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/localization/5148" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/loc...</a></p>
</blockquote>
</div></template>


